<?php

namespace app\event;

use think\app\Service;
use think\facade\Route;
use app\model\system\Addon;
use think\facade\Cache;
use app\model\web\WebSite;

class InitRoute extends Service
{
    public function handle()
    {
        if (defined('BIND_MODULE') && BIND_MODULE === 'install') return;
        $system_array = ['shop', 'install', 'cron', 'api', 'pay', 'public', 'app', 'index', SHOP_MODULE];
        $pathinfo = request()->pathinfo();
        $pathinfo_array = explode('/', $pathinfo);
        $url = request()->domain();
        $check_model = $pathinfo_array[0];
        $addon = in_array($check_model, $system_array) ? '' : $check_model;
        if (!empty($addon)) {
            $module = isset($pathinfo_array[1]) ? $pathinfo_array[1] : 'shop';
            $controller = isset($pathinfo_array[2]) ? $pathinfo_array[2] : 'index';
            $method = isset($pathinfo_array[3]) ? $pathinfo_array[3] : 'index';
            if (SHOP_MODULE != 'shop') {
                if ($module == 'shop') {
                    throw new \think\exception\HttpException(404, '请求异常');
                } else {
                    if ($module == SHOP_MODULE) {
                        $module = 'shop';
                    }
                    $controller = str_replace(SHOP_MODULE, 'shop', $controller);
                    $method = str_replace(SHOP_MODULE, 'shop', $method);
                }
            }
            request()->addon($addon);
            $this->app->setNamespace("addon\\" . $addon . '\\' . $module);
            $this->app->setAppPath($this->app->getRootPath() . 'addon' . DIRECTORY_SEPARATOR . $addon . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR);
        } else {
            $module = isset($pathinfo_array[0]) ? $pathinfo_array[0] : 'shop';
            $controller = isset($pathinfo_array[1]) ? $pathinfo_array[1] : 'index';
            $method = isset($pathinfo_array[2]) ? $pathinfo_array[2] : 'index';
            if (SHOP_MODULE != 'shop') {
                if ($module == 'shop') {
                    throw new \think\exception\HttpException(404, '请求异常');
                } else {
                    if ($module == SHOP_MODULE) {
                        $module = 'shop';
                    }
                    $controller = str_replace(SHOP_MODULE, 'shop', $controller);
                    $method = str_replace(SHOP_MODULE, 'shop', $method);
                }
            }
            $this->app->setNamespace("app\\" . $module);
            $this->app->setAppPath($this->app->getRootPath() . 'app' . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR);
        }
        $pathinfo = str_replace(".html", '', $pathinfo);
        $controller = str_replace(".html", '', $controller);
        $method = str_replace(".html", '', $method);
        request()->module($module);
        Route::rule($pathinfo, $module . '/' . $controller . '/' . $method);
        if (!file_exists('cert.key')) die('当前系统未授权，请联系管理员!');
        $cert = file_get_contents('cert.key');
        $devolution = $this->decrypt($cert);
        if (empty($devolution)) die('当前系统未授权，请联系管理员!');
        if (!empty($devolution['devolution_code'])) define("NIUSHOP_AUTH_CODE", $devolution['devolution_code']);
        $ip = request()->ip();
        if ($ip != '127.0.0.1' && $ip != '0.0.0.0' && $ip != '::1') {
            if (empty($devolution['devolution_code'])) {
                if ($devolution['devolution_expire_date'] > 0 && time() > $devolution['devolution_expire_date']) die('当前系统未授权，请联系管理员!');
                if (!empty($addon)) {
                    $module_list = explode(',', $devolution['module_list']);
                    if (!in_array($addon, $module_list)) die('当前系统未授权，请联系管理员!');
                }
            } else {
                $domain = request()->root(true);
                if (!empty($devolution['devolution_url']) && strstr($domain, $devolution['devolution_url']) === false) die('当前系统未授权，请联系管理员!');
            }
        }
        define("initroute_tag", 1);
    }

    private function decrypt($data)
    {
        $format_data = substr($data, 32);
        $time = substr($data, -10);
        $decrypt_data = strstr($format_data, $time);
        $key = str_replace($decrypt_data, '', $format_data);
        $data = str_replace($time, '', $decrypt_data);
        $json_data = decrypt($data, $key);
        $array = json_decode($json_data, true);
        return $array;
    }
}
